package com.go.where.manager.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.go.where.manager.common.Constants;
import com.go.where.manager.dao.VideoDao;
import com.go.where.manager.page.Pagination;
import com.go.where.manager.page.BaseQuery.OrderType;
import com.go.where.manager.pojo.VideoInfo;
import com.go.where.manager.pojo.user.UserInfoVO;
import com.go.where.manager.service.VideoService;

@Service("videoService")
public class VideoServiceImpl implements VideoService {

	@Resource(name = "videoDao")
	private VideoDao videoDao;

	@Override
	public VideoInfo queryByPrimartKey(String cid) {
		return videoDao.getByKey(cid);
	}

	@Override
	public Pagination<VideoInfo> queryPage(VideoInfo videoInfo) {
		if (StringUtils.isNotEmpty(videoInfo.getOrderBy())
				&& videoInfo.getOrderType() != null) {
			if (videoInfo.getOrderType() == OrderType.asc) {
				videoInfo.setPage_orderByAsc(videoInfo.getOrderBy());

			} else {
				videoInfo.setPage_orderByDesc(videoInfo.getOrderBy());
			}
		}
		Integer count = videoDao.totalCount(videoInfo);
		videoInfo.calculate();
		List<VideoInfo> list = videoDao.queryList(videoInfo);
		Pagination<VideoInfo> page = videoDao.queryPagination(count, list, videoInfo);
		return page;
	}

	@Override
	public int save(VideoInfo videoInfo, UserInfoVO currentUser) {
		videoInfo.setPublishTime(new Date());
		videoInfo.setUserId(currentUser.getUserId());
		videoInfo.setIsDelete(Constants.NOT_DELETED);
		return videoDao.insert(videoInfo);
	}

	@Override
	public int update(VideoInfo videoInfo, UserInfoVO currentUser) {
		videoInfo.setUpdateTime(new Date());
		videoInfo.setUserId(currentUser.getUserId());
		return videoDao.update(videoInfo);
	}

}
